package com.hackordie.commons.software.applications;

import java.util.Random;

import com.hackordie.commons.hardware.components.Firewall;

public class FirewallDesencryptor extends AApplication {

    private int desencryptedKey; 
    
    public FirewallDesencryptor(String name, String description, int size,
            int requiredRam, int requiredProcessor) {
        super(name, description, size, requiredRam, requiredProcessor);
    }

    @Override
    public boolean run(Object[] obj) {
        
       
        Firewall firewallToDesencrypt = (Firewall) obj[1];
        Integer keyToTry = (Integer) obj[2];
        
        // si sabemos la clave, entramos automaticamente
        if (firewallToDesencrypt.getAccesKey() == keyToTry) {
            desencryptedKey = keyToTry;
            return true;
        }

        Random rnd = new Random();

        if (rnd.nextInt(99) > firewallToDesencrypt.getDificultAccess()) {
            desencryptedKey = firewallToDesencrypt.getAccesKey();
            return true;
        }

        // return indicando que no ha podido descifrarla
        return false;
    }
    
    public int getDesencryptedKey() {
        return desencryptedKey;
    }

    @Override
    public boolean checkObj(Object[] obj) {
        if( !(obj.length == 3              &&
                obj[1] instanceof Firewall && 
                obj[2] instanceof Integer)){
             
            return false;
          }
        return true;
    }
}
